#include<iostream>
using namespace std;

/*
删除链表中等于给定值val的所有节点。

输入：head=[1,2,6,3,4,5,6],val=6 输出：[1,2,3,4,5]
*/

class node{
public:
    int val;
    node*next;
    node(int v){
        val=v;
        next=0;
    };
    node(){
        val=0;
        next=0;
    }
};

class chain{
public:
    node*head;
    int length;
    chain(){
        head=0;
        length=0;
    }
    void del(int v);
    void append(int v);
    void output();
    ~chain();
};

void chain::del(int v){
    node*dummyHead=new node();//设置虚拟头节点，无需在删除时考虑头节点的删除问题
    dummyHead->next=this->head;
    node*ptr=dummyHead;

    while(ptr->next!=0){
        if(ptr->next->val==v){
            node*temp=ptr->next;
            ptr->next=temp->next;
            delete temp;
            length--;
        }
        else
            ptr=ptr->next;
    }
    head=dummyHead->next;//定位头节点
    delete dummyHead;
}

void chain::append(int v){
    if(head==0)
        head=new node(v);
    else{
        node*ptr=head;
        while(ptr->next!=0){
            ptr=ptr->next;
        }
        ptr->next=new node(v);
    }
    length++;
}

void chain::output(){
    node*ptr=head;
    while(ptr!=0){
        node*temp=ptr;
        ptr=ptr->next;
        cout<<temp->val<<" ";
    }
    cout<<endl;
}

chain::~chain(){
    node*ptr=head;
    while(ptr!=0){
        node*temp=ptr;
        ptr=ptr->next;
        delete temp;
    }
}

int main(){
    int array[7]={5,6,2,6,1,6,6};
    chain c;
    for(int i=0;i<7;i++){
        c.append(array[i]);
    }
    c.output();
    c.del(6);
    c.output();
    return 0;
}